﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandClear.Util.Caching;

namespace thousandClear.Areas.Admin.Controllers
{
    public class BaseController : Controller
    {
        public void SetCookie(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name); 

            cookie.Value = value;
            cookie.Expires = DateTime.Now.AddHours(2);

            ControllerContext.HttpContext.Response.Cookies.Add(cookie);
        }

        public string GetCookie(string name) 
        {
            HttpCookie cookie = ControllerContext.HttpContext.Request.Cookies.Get(name);

            if (cookie != null)
            {
                return cookie.Value;
            }

            return string.Empty;
        }

        public void ClearCookie()
        {
            ControllerContext.HttpContext.Request.Cookies.Clear();
        }

        public bool CheckLogin()
        {
            string sessionId = GetCookie("SessionId");

            if (string.IsNullOrEmpty(sessionId))
            {
                return false;
            }

            if (!CacheProvider.Exist(sessionId))
            {
                return false;
            }

            var data = (AdminCacheData)CacheProvider.Get(sessionId);

            if (data == null)
                return false;

            return data.LoginStatus;
        }

       
    }
}
